/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSTranslate.cpp
 *
 *  Created on: 2010/03/04
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#include "RSTranslate.h"
#include "RSSerializer.h"
#include <QXmlStreamWriter>

namespace Realsphere {

    RSTranslate::RSTranslate ( const QString& rScopeId ) :
        RSTransformationElement ( RS_TRANSLATE, rScopeId )
    {
    }

    RSTranslate::~RSTranslate ()
    {
    }

    QVector3D RSTranslate::Position () const
    {
        return mPosition;
    }

    void RSTranslate::SetPosition ( const QVector3D& rPosition, bool rEmit )
    {
        mPosition = rPosition;

        SetDirty ( true );

        if ( rEmit )
            emit PositionChanged ( mPosition );
    }

    void RSTranslate::SetPosition ( qreal rX, qreal rY, qreal rZ, bool rEmit )
    {
        QVector3D position ( rX, rY, rZ );
        SetPosition ( position, rEmit );
    }

    void RSTranslate::ApplyTranslation ( const QVector3D& rDisplacement )
    {
        SetPosition ( mPosition + rDisplacement );
    }

    void RSTranslate::ApplyTranslation ( qreal rX, qreal rY, qreal rZ )
    {
        QVector3D displacement ( rX, rY, rZ );
        ApplyTranslation ( displacement );
    }

    void RSTranslate::WriteElementData ( QXmlStreamWriter *rXmlWriter )
    {
        rXmlWriter->writeCharacters ( RSSerializer::serialize ( mPosition ) );
    }

    void RSTranslate::UpdateTransformation ()
    {
        QMatrix4x4 transformation;
        transformation.translate ( mPosition );
        SetTransformation ( transformation );
        SetDirty ( false );
    }
}
